<template>
	<view style="padding: 0 10rpx;">
		<uni-forms ref="form" :rules="rules" :model="updateForm">
			<uni-forms-item name="mobilePhone" v-if="type===1">
				<uni-easyinput v-model="updateForm.mobilePhone" type="number" trim="both" :maxlength="11" :focus="true"
					placeholder="请输入新的联系方式" />
			</uni-forms-item>
			<uni-forms-item name="phone" v-if="type===2">
				<uni-easyinput v-model="updateForm.phone" trim="both" :focus="true" placeholder="请输入新的固定电话" />
			</uni-forms-item>
			<uni-forms-item name="email" v-if="type===3">
				<uni-combox v-model="updateForm.email" trim="both" :candidates="candidates" :focus="true"
					placeholder="请输入新的电子邮箱" />
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	import {
		userStore
	} from "@/store/user.js";
	import {
		throttle
	} from "lodash";
	import {
		UpdateUserInfo
	} from "@/apis/user.js";

	export default {
		data() {
			return {
				type: "1",
				typeObj: {
					1: "联系方式",
					2: "固定电话",
					3: "电子邮箱"
				},
				candidates: [],
				rules: {
					mobilePhone: {
						rules: [{
								required: true,
								errorMessage: "请输入联系方式"
							},
							{
								minLength: 11,
								maxLength: 11,
								errorMessage: "联系方式应为11位"
							},
							{
								pattern: /^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/,
								errorMessage: "联系方式格式错误"
							}
						]
					},
					phone: {
						rules: [{
								required: true,
								errorMessage: "请输入固定电话"
							},
							{
								minLength: 12,
								maxLength: 12,
								errorMessage: "固定电话应为12位"
							},
							{
								pattern: /^(0[0-9]{2,3})?([2-9][0-9]{6,7})+([0-9]{1,4})?$/,
								errorMessage: "固定电话格式错误"
							}
						]
					},
					email: {
						rules: [{
								require: true,
								errorMessage: "请输入电子邮箱"
							},
							{
								pattern: /^[A-Za-z\d]+([-_\.][A-Za-z\d]+)*@([A-Za-z\d]+[-\.])+[A-Za-z\d]{2,4}/,
								errorMessage: "电子邮箱格式错误"
							}
						]
					}
				},
				updateForm: {
					mobilePhone: "",
					phone: "",
					email: ""
				},
				mails: ["163.com", "126.com", "qq.com", "google.com", "wzut.edu.cn", "dingtalk.com"]
			}
		},
		watch: {
			updateForm: {
				handler(newVal) {
					this.candidates = [];
					if (!newVal.email.split("").includes("@"))
						this.mails.forEach(item => {
							this.candidates.push(`${newVal.email}@${item}`)
						})
					else {
						this.candidates.push(newVal.email);
					}
				},
				immediate: true,
				deep: true
			}
		},
		onLoad({
			type
		}) {
			this.type = Number.parseInt(type);
			uni.setNavigationBarTitle({
				title: `编辑${this.typeObj[type]}`
			});
		},
		onNavigationBarButtonTap() {
			const useUserStore = userStore();
			this.$refs.form.validate().then(() => {
				UpdateUserInfo(this.updateForm).then((res) => {
					// console.log(res);
					useUserStore.setUserInfo(res.data);
					uni.showToast({
						icon: "success",
						title: "更新成功"
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						});
					}, 1000)
				})
			})
		}
	}
</script>

<style lang="scss" scoped>

</style>
